package com.crm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.Contact;
import com.crm.services.ContactServices;
import com.crm.services.DictionaryServices;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("customer")
public class ContactController {
	
	@Autowired 
	ContactServices cons;//联系人服务类
	@Autowired
	private DictionaryServices ds;//字典服务类
	
	@RequestMapping("/contacts")
	public void contacts() {
		System.out.println("get联系人");
	}
	
	@RequestMapping("/merDay")
	public void customerMerger() {
		System.out.println("get纪念日");
	}

	@PostMapping("/contactPage")
	@ResponseBody	
	public PageInfo<Map<String, String>> contactPage(Integer pageNum, Integer pageSize) {
		System.out.println("联系人+++");
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAllContact = cons.selectContact(pageNum, pageSize);
		//List<Map<String, String>> selectAllContact = cons.selectByPager(pageNum, pageSize);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAllContact);
		System.out.println(pager);
		return pager;
	}
	
	@PostMapping("/merdayPage")
	@ResponseBody	
	public PageInfo<Map<String, String>> merdayPage(Integer pageNum, Integer pageSize) {
		System.out.println("联系人+++");
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		//List<Map<String, String>> selectAllContact = cons.selectContact(pageNum, pageSize);
		List<Map<String, String>> selectAllContact = cons.selectMerday(pageNum, pageSize);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAllContact);
		System.out.println(pager);
		return pager;
	}
	
	@GetMapping("/newContacts")
	public void addContact() {
		System.out.println("get添加联系人");
	}
	
	/**
	 * 添加一个联系人信息
	 * @param name
	 * @param phone
	 * @param counterpart 上级联系人
	 * @param catalogue 联系人分类
	 * @param operations 负责业务
	 * @param sex
	 * @param appellation 称谓
	 * @param email
	 * @param code
	 * @param address
	 * @param dtype 证件类型
	 * @param number
	 * @param interest
	 * @param remarks
	 * @param habit
	 * @param character
	 * @param social
	 * @param income
	 * @param conhabit
	 * @param persize
	 * @param url
	 * @param merday
	 * @return
	 */
	@PostMapping("/newContacts")
	@ResponseBody
	public String addCon(@RequestParam(name="name",required=false) String name,
			@RequestParam(name="customer",required=false) Integer customer,
			@RequestParam(name="phone",required=false) String phone,
			@RequestParam(name="counterpart",required=false) Integer counterpart,
			@RequestParam(name="catalogue",required=false) Integer catalogue,
			@RequestParam(name="operations",required=false) String operations,
			@RequestParam(name="sex",required=false) Integer sex,
			@RequestParam(name="appellation",required=false) String appellation,
			@RequestParam(name="email",required=false) String email,
			@RequestParam(name="code",required=false) String code,
			@RequestParam(name="address",required=false) String address,
			@RequestParam(name="dtype",required=false) Integer dtype,
			@RequestParam(name="dnumber",required=false) String number,
			@RequestParam(name="interest",required=false) Integer interest,
			@RequestParam(name="remarks",required=false) String remarks,
			@RequestParam(name="habit",required=false) Integer habit,
			@RequestParam(name="character",required=false) Integer character,
			@RequestParam(name="social",required=false) Integer social,
			@RequestParam(name="income",required=false) Integer income,
			@RequestParam(name="conhabit",required=false) Integer conhabit,
			@RequestParam(name="persize",required=false) Integer persize,
			@RequestParam(name="url",required=false) String url)
	{
		System.out.println(name+"--"+customer+"--"+counterpart+"--"+sex+"--"+phone);
		Contact contact=new Contact(0, customer, income, appellation, code, catalogue, character, name, conhabit, counterpart, address, email, sex, phone, number, dtype, conhabit, interest, null, null, operations, persize, null, remarks, social, url,1206);
		cons.addCon(contact);
		return "0000";
	}
	
	@GetMapping("/newMerday")
	public void addMerday() {
		System.out.println("get添加纪念日");
	}
	
	/**
	 * 新建纪念日
	 * @param name
	 * @param phone
	 * @param counterpart
	 * @param catalogue
	 * @param operations
	 * @param sex
	 * @param appellation
	 * @param email
	 * @param code
	 * @param address
	 * @param dtype
	 * @param number
	 * @param interest
	 * @param remarks
	 * @param habit
	 * @param character
	 * @param social
	 * @param income
	 * @param conhabit
	 * @param persize
	 * @param url
	 * @param merday
	 * @return
	 */
	@PostMapping("/newMerday")
	@ResponseBody
	public String addMer(@RequestParam(name="name",required=false) String name,
			@RequestParam(name="customer",required=false) Integer customer,
			@RequestParam(name="mtype",required=false)String mtype,
			@RequestParam(name="merday",required=false)Date merday,
			@RequestParam(name="remarks",required=false) String remarks)
	{
		
		Contact contact=new Contact(0, customer, null, null, null, null, null, name, null, null, null, null, null, null, null, null, null, null, merday, mtype, null, null, null, remarks, null, null,1206);
		cons.addCon(contact);
		return "0000";
	}
	  
	/**
	 * 联系人字典表
	 * @return
	 */
	@RequestMapping("/contactDictInfo")
	@ResponseBody
	public JSONArray contactDictInfo() {
		JSONArray array=new JSONArray();
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1000)));//联系人分类
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1004)));//性别
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1007)));//证件类型
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1012)));//习惯
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1017)));//兴趣爱好
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1031)));//性格
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1039)));//社交特点
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1049)));//年收入
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1056)));//消费习惯
		array.add(ds.ListTransJSONArray(ds.findByDictParentid(1060)));//人员规模
		return array;
	}
	
	/**
	 * 删除联系人
	 * @param conid
	 * @return
	 */
	@RequestMapping("/deleteContact")
	public int deleteContact(@RequestParam("ID") Integer conid) {
		System.out.println("联系人编号："+conid);
		Object deleteContact = cons.deleteContact(conid);
		return 1;
	}
	
	/**
	 * 根据id查询联系人
	 * @param conid
	 * @return
	 */
	@PostMapping("/selectOneContact")
	@ResponseBody
	public String selectOneContact(@RequestParam("conId") Integer conid) {
		System.out.println("联系人编号："+conid);
		Map<String, String> queryContact = cons.queryContact(conid);
		String jsonString = JSONObject.toJSONString(queryContact);
		System.out.println(jsonString);
		return jsonString;
	}
	
	//查询纪念日信息
	@PostMapping("/selectMerday")
	@ResponseBody
	public String selectMerday(@RequestParam("conId") Integer conid) {
		Map<String, String> seletMerday = cons.seletMerday(conid);
		String jsonString = JSONObject.toJSONString(seletMerday);
		System.out.println("纪念日信息："+jsonString);
		return jsonString;
	}
	
	/**
	 * 修改客户信息
	 * @param con
	 * @return
	 */
	@PostMapping("/updateContact")
	@ResponseBody
	public int updateContact(@ModelAttribute("con") Contact con) {
		System.out.println("修改客户信息："+con);
		Object updateContact = cons.updateContact(con);
		return 1;
	}
	
	//修改纪念日信息
	@PostMapping("/updateMerday")
	@ResponseBody
	public int updateMerday(@ModelAttribute("con") Contact con) {
		System.out.println("修改纪念日信息："+con);
		Object updateMerday = cons.updateMerday(con);
		return 1;
	}
	
	@RequestMapping("/findByConName")
	@ResponseBody
	public Integer findByConName(String conName) {
		System.out.println("触发确认联系人姓名是否重复:"+conName);
		return cons.findByConName(conName);
	}
	
	/**
	 * 联系人条件查询
	 * @param pageNum
	 * @param pageSize
	 * @param value7
	 * @return
	 */
	@RequestMapping("/selectBycon")
	@ResponseBody
	public PageInfo<Map<String, String>> selectBycon(Integer pageNum, Integer pageSize,
			@RequestParam(name="value7",required=false) Integer value7) {
		System.out.println("联系人条件查询+++");
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		PageInfo<Map<String, String>> pager = null;//分页
		if (value7==7878) {
			List<Map<String, String>> selectByPager = cons.selectAllContact(pageNum, pageSize, value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectByPager);
		}else if(value7>1000 && value7<=1003) {
			List<Map<String, String>> selectCatalogue = cons.selectCatalogue(pageNum, pageSize,value7);//客户种类
			//分页
			pager=new PageInfo<Map<String, String>>(selectCatalogue);
		}else if(value7>1004 & value7<=1006) {
			List<Map<String, String>> selectCustsex = cons.selectCustsex(pageNum, pageSize,value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectCustsex);
		}else if(value7>1007 & value7<=1011) {
			List<Map<String, String>> selectDtype = cons.selectDtype(pageNum, pageSize,value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectDtype);
		}
		return pager; 
	}
	
	@RequestMapping("/merdayBycon")
	@ResponseBody
	public PageInfo<Map<String, String>> merdayBycon(Integer pageNum, Integer pageSize,
			@RequestParam(name="value7",required=false) Integer value7) {
		System.out.println("纪念日条件查询+++");
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		PageInfo<Map<String, String>> pager = null;//分页
		if (value7==7788) {
			List<Map<String, String>> selectByPager = cons.selectAll(pageNum, pageSize, value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectByPager);
		}else if(value7==7789) {
			List<Map<String, String>> selectCatalogue = cons.selectDay(pageNum, pageSize, value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectCatalogue);
		}else if(value7==7790) {
			List<Map<String, String>> selectCustsex = cons.selectMonth(pageNum, pageSize, value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectCustsex);
		}else if(value7==7791) {
			List<Map<String, String>> selectDtype = cons.selectNext(pageNum, pageSize, value7);
			//分页
			pager=new PageInfo<Map<String, String>>(selectDtype);
		}
		return pager; 
	}
	
	//模糊查询联系人姓名
	@GetMapping("/conLike")
	@ResponseBody	
	public PageInfo<Map<String, String>> conLike(Integer pageNum, Integer pageSize,
			@RequestParam(name="input5",required=false) String input5) {
		System.out.println("模糊查询联系人+++");
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectCNameLike = cons.selectCNameLike(pageNum, pageSize, input5);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectCNameLike);
		System.out.println("模糊查询联系人的信息："+pager);
		return pager;
	}
}
